package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Description NursingPlanServiceImpl
 * @Author xun
 * @Date 2025-08-31
 */
@Service
public class NursingPlanServiceImpl implements NursingPlanService {
    @Override
    public void updateNursingPlan(NursingPlanVo nursingPlanVo) {

    }

/*    @Override
    public void updateById(NursingProjectPlanVo nursingProjectPlanVo) {

    }*/

    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    @Override
    public PageResponse<NursingPlanVo> getByPage(String planName, Integer status, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlan> nursingPlan = nursingPlanMapper.selectByPage(planName, status);
        PageResponse<NursingPlanVo> planVoPageResponse = PageResponse.of(nursingPlan, NursingPlanVo.class);
        return planVoPageResponse;
    }

    @Override
    public List<NursingPlanVo> findAll() {

        List<NursingPlanVo> nvlist=nursingPlanMapper.findAll();
        return nvlist;
    }

    @Override
    public NursingPlan findById(Integer id) {
        NursingPlan np = nursingPlanMapper.findById(id);

        return np;
    }

    @Override
    public void add(NursingPlanVo plan ) {



        List<NursingProjectPlan> projectPlans = BeanUtil.copyToList(plan.getProjectPlans(), NursingProjectPlan.class);
        NursingPlanVo bean = BeanUtil.toBean(plan, NursingPlanVo.class);
        bean.setStatus(0);
        nursingPlanMapper.createNursingPlan(bean);
        projectPlans.forEach(e->e.setPlanId((bean.getId())));
        nursingPlanMapper.createNursingProjectPlan(projectPlans);

    }

    @Override
    public NursingPlanVo getNursingPlanById(Long id) {
        return null;
    }

    @Override
    public NursingPlanVo createNursingPlan(NursingPlanDto nursingPlanDto) {
        return null;
    }

   /* @Override
    public void updateById(NursingProjectPlanVo nursingProjectPlanVo) {
        //先修改护理计划基本信息，护理计划名称
        nursingPlanMapper.updatePlan(nursingProjectPlanVo);
        //再根据某一个护理项目的id去修改护理项目的信息
        //拿到这个护理计划的护理项目列表
        *//*List<NursingProjectPlan> projectPlans = NursingProjectPlanVo.getProjectPlans();*//*
        //遍历
        for (NursingProjectPlan project : projectPlans) {
            nursingPlanMapper.updateProjectsPlan(project);
        }
    }*/



    @Override
    public void deleteNursingPlan(Long id) {
        nursingPlanMapper.deleteById(id);

    }

    @Override
    public void updateNursingPlanStatus(Long id, Integer status) {
        nursingPlanMapper.changeStatus(id,status);

    }

    @Override
    public List<NursingPlanVo> searchNursingPlans(String planName, Integer status) {
        return List.of();
    }

    @Override
    public void updateById(NursingProjectPlanVo nursingProjectPlanVo) {
        //先修改护理计划基本信息，护理计划名称
        nursingPlanMapper.updatePlan(nursingProjectPlanVo);
        //再根据某一个护理项目的id去修改护理项目的信息
        //拿到这个护理计划的护理项目列表
        List<NursingProjectPlan> projectPlans = nursingProjectPlanVo.getProjectPlans();
        //遍历
        for (NursingProjectPlan project : projectPlans){
            nursingPlanMapper.updateProjectsPlan(project);
        }
    }


}
